package cn.tedu.store.controller;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;

import cn.tedu.store.bean.ResponseResult;
import cn.tedu.store.bean.User;
import cn.tedu.store.service.IUserService;
import cn.tedu.store.service.ex.PasswordNotMatchException;
import cn.tedu.store.service.ex.UserNotFoundException;
import cn.tedu.store.service.ex.UsernameAlreadyExistException;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	@Resource
	private IUserService userService;
	
	/**
	 * 上传图片
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/uploadImage.do")
	@ResponseBody
	public ResponseResult<Void> uploadImage(
			MultipartFile file,
			HttpSession session) throws IllegalStateException, IOException{
		System.out.println("uploadImage");
		ResponseResult<Void> rr = 
				new ResponseResult<Void>(1,"上传成功");
		//获取服务器的真实路径
		String contextPath = session.getServletContext().
				getRealPath("/upload");
		String path = contextPath+"/"+file.getOriginalFilename();
		
		System.out.println(path);
		//上传 文件到指定的路径
		file.transferTo(new File(path));
		userService.updateImageById(this.getUid(session), "/upload/"+file.getOriginalFilename());
		return rr;
	}
	/**
	 * 显示注册画面
	 * @return
	 */
	@RequestMapping("/showRegister.do")
	public String showRegister(){
		return "register";
	}
	
	/**
	 * 显示登录画面
	 * @return
	 */
	@RequestMapping("/showLogin.do")
	public String showLogin(){
		return "login";
	}
	/**
	 * 显示修改密码页面
	 * @return
	 */
	@RequestMapping("/showPassword.do")
	public String showPassword(){
		return "password";
	}
	
	/**
	 * 显示个人信息的页面
	 * @return
	 */
	@RequestMapping("/showPerson.do")
	public String showPerson(){
		return "person";
		
	}
	
	
	@RequestMapping("/checkUsername.do")
	@ResponseBody
	public ResponseResult<Void> checkUsername(String username){
		ResponseResult<Void> rr;
		//如果返回true,表示用户名存在,
		//如果返回false,表示用户名不存在
		if(userService.checkUsername(username)){
			rr = new ResponseResult<Void>(0,"用户名已经存在");
		}else{
			rr = new ResponseResult<Void>(1,"用户名可以使用");
		}
		return rr;
	}
	@RequestMapping("/checkEmail.do")
	@ResponseBody
	public ResponseResult<Void> checkEmail(String email){
		ResponseResult<Void> rr;
		//如果返回true,表示用户名存在,
		//如果返回false,表示用户名不存在
		if(userService.checkEmail(email)){
			rr = new ResponseResult<Void>(0,"邮箱已经存在");
		}else{
			rr = new ResponseResult<Void>(1,"邮箱可以使用");
		}
		return rr;
	}
	@RequestMapping("/checkPhone.do")
	@ResponseBody
	public ResponseResult<Void> checkPhone(String phone){
		ResponseResult<Void> rr;
		//如果返回true,表示用户名存在,
		//如果返回false,表示用户名不存在
		if(userService.checkPhone(phone)){
			rr = new ResponseResult<Void>(0,"电话号码已经存在");
		}else{
			rr = new ResponseResult<Void>(1,"电话号码可以使用");
		}
		return rr;
	}
	
	@RequestMapping("/register.do")
	@ResponseBody
	public ResponseResult<Void> register(
		@RequestParam("uname") String username,
		@RequestParam("upwd") String password,
		String email,String phone){
		
		ResponseResult<Void> rr;
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setPhone(phone);
		user.setEmail(email);
		//set方法给User对象赋值
		try{
			userService.register(user);
			rr = new ResponseResult<Void>(1,"注册成功");
		}catch(UsernameAlreadyExistException e){
			rr = new ResponseResult<Void>(0,e.getMessage());
		}
		return rr;
	}
	
	@RequestMapping("/login.do")
	@ResponseBody
	public ResponseResult<Void> login(
			String username,String password,HttpSession session){
		ResponseResult<Void> rr;
		try{
			User user = userService.login(username, password);
			session.setAttribute("user",user);
			rr = new ResponseResult<Void>
			(1,"登录成功");
		}catch(UserNotFoundException e){
			rr = new ResponseResult<Void>
			(0,e.getMessage());
		}catch(PasswordNotMatchException e){
			rr = new ResponseResult<Void>
			(-1,e.getMessage());
		}
		return rr;
	}
	@RequestMapping("/logout.do")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:../main/showIndex.do";
	}
	
	@RequestMapping("/updatePassword.do")
	@ResponseBody
	public ResponseResult<Void> updatePassword(HttpSession session,
			String newPassword,String oldPassword){
		ResponseResult<Void> rr;
		try {
			Integer id=this.getUid(session);
			userService.changePassword(id, oldPassword, newPassword);
			rr=new ResponseResult<Void>(1, "修改成功");
		} catch (RuntimeException e) {
			rr=new ResponseResult<Void>(0,e.getMessage());
		}
		return rr;
	}
	
	@RequestMapping("/updatePerson.do")
	@ResponseBody
	public ResponseResult<Void> updatePerson(
			HttpSession session,String username,
			Integer gender,String phone,
			String email){
		
		ResponseResult<Void> rr;
		try{
			Integer id = this.getUid(session);
			userService.updatePerson(id, username, gender, phone, email);
			//把修改后的对象重新设置为session的属性
			session.setAttribute("user", userService.getUserById(id));
			
			rr = new ResponseResult<Void>(1,"修改成功");
			
		}catch(RuntimeException e){
			rr = new ResponseResult<Void>(0,e.getMessage());
		}
		return rr;
	}
	
	
	
	
	
	
	
	
	

	/**
	 * 下载资源
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/demo.do",produces="image/png")
	@ResponseBody
	public byte[] demo(HttpServletResponse response) throws IOException{
//		response.setContentType("image/png");
		
		
		/*response.setHeader("Content-Disposition", "attachment;filename=\"ok.png\"");*/
		//处理中文
		String filename=URLEncoder.encode("图片.png", "utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=\""+filename+"\"");
		//获取图片的数据
		byte[] body=getImage();
		return body;
	}
	private byte[] getImage() throws IOException{
		BufferedImage img=new BufferedImage(100,30,BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g=img.createGraphics();
		g.setColor(Color.RED);
		g.drawString("Hello World", 3, 28);
		//“酒瓶”用于装入byte数据
		ByteArrayOutputStream out=new ByteArrayOutputStream();
		//将png图片的数据(byte)装入“酒瓶out”中
		ImageIO.write(img, "png", out);
		//为“酒瓶”盖上盖子
		out.close();
		//从“酒瓶”中到处byte数据
		byte[] bytes=out.toByteArray();
		return bytes;
		
	}
	/**
	 * 下载Excel
	 */
	@RequestMapping("/excel.do")
	@ResponseBody
	public byte[] excel(HttpServletResponse response)throws Exception{
		response.setContentType(
			"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		String filename=URLEncoder
			.encode("表格.xlsx","utf-8");
		response.setHeader(
			"Content-Disposition",
			"attachment; filename=\""+
			filename+"\"");
		byte[] body = getExcel();
		return body;
	}
	
	/**
	 * 生成Excel文件
	 */
	public byte[] getExcel()
		throws IOException{
		//工作簿: 就是一个Excel文件对象
		XSSFWorkbook workbook = 
			new XSSFWorkbook();
		//在工作簿创建一个 Hello 工作表
		XSSFSheet sheet=
			workbook.createSheet("Hello");
		//在工作表中添加一行: 参数是行编号 0 1 2 
		//参数相同的话, 会覆盖以前的行
		XSSFRow row = sheet.createRow(0);
		//在行中可以添加很多格子,参数是格子的位置序号
		XSSFCell cell = row.createCell(0);
		//在格子中添加数据
		cell.setCellValue("Hello World!");
		ByteArrayOutputStream out=
			new ByteArrayOutputStream();
		workbook.write(out); 
		workbook.close();
		out.close();
		byte[] bytes = out.toByteArray();
		return bytes;
	}
}










